package interview;

import java.util.Scanner;
import java.util.function.IntConsumer;

class ZeroEvenOdd{
    private int n;
    private int num=1;
    public ZeroEvenOdd(int n){
        this.n=n;
    }
    final static Object condition_1=new Object();
    final static Object condition_2=new Object();

    public void wait(Object object){
        synchronized (object) {
            try {
                object.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void release(Object object){
        synchronized (object){
            object.notify();
        }
    }
    public boolean end(){
        return num==n+1;
    }
    public void printZero(IntConsumer printNumber)throws InterruptedException{
        while(true) {
            if(end()){
                release(condition_2);// 另外两个线程执行完必然释放condition_1，该线程是最先结束，所以还需要释放condition_2,这样另外两个线程才能释放。
                break;
            }
            printNumber.accept(0);
            release(condition_2);
            wait(condition_1);
        }

    }
    public void printEven(IntConsumer printNumber)throws InterruptedException{

        while(true){
            wait(condition_2);
            if(end()){
                release(condition_2);
                break;
            }
            if(num%2==0){
                printNumber.accept(num);
                num++;
            }else {
                release(condition_2);
                continue;
            }
            release(condition_1);
        }
    }
    public void printOdd(IntConsumer printNumber)throws InterruptedException{
        while(true){
            wait(condition_2);
            if(end()){
                release(condition_2);
                break;
            }
            if(num%2==1){
                printNumber.accept(num);
                num++;
            }else {
                release(condition_2);
                continue;
            }
            release(condition_1);
        }
    }

}
public class Main {
    public static void main(String[] args) {
        final Scanner reader=new Scanner(System.in);
        int n=reader.nextInt();
        ZeroEvenOdd zeroEvenOdd=new ZeroEvenOdd(n);
        new Thread(()->{
            try {
                zeroEvenOdd.printZero(System.out::print);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
        new Thread(()->{
            try {
                zeroEvenOdd.printEven(System.out::print);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
        new Thread(()->{
            try {
                zeroEvenOdd.printOdd(System.out::print);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
    }

}
